import os
import os.path as osp
from pathlib import Path
import json
import yaml

from qtpy import QtGui

here = osp.dirname(osp.abspath(__file__))

def newIcon(icon):
    icons_dir = osp.join(here, "../icons")
    return QtGui.QIcon(osp.join(":/", icons_dir, "%s.png" % icon))

def read_json(path, mode='r'):
    with open(path, mode, encoding='utf8') as fp:
        data = json.load(fp)
    return data

def write_json(path, data, mode='w'):
    with open(path, mode, encoding="utf-8") as fp:
        json.dump(data, fp=fp, ensure_ascii=False, indent=2)

def read_txt(path, mode='r'):
    with open(path, mode, encoding='utf8') as fp:
        data = fp.readlines()
    
    return data

def write_txt(path, data, mode='a'):
    with open(path, mode, encoding='utf8') as fp:
        fp.writelines(data)

def read_yaml(path, mode='r'):
    with open(path, mode=mode, encoding="utf-8") as fp:
        data = yaml.safe_load(fp)
    
    return data

def filter_exist_file(all_path_list, exist_path_list):
    all_path_list = [Path(path) for path in all_path_list]
    exist_name_list = set([Path(path).stem for path in exist_path_list])
    return_path_list = []
    for path in all_path_list:
        if not path.stem in exist_name_list:
            return_path_list.append(str(path))
    # print(f"===> 该批次总数: {len(all_path_list)}; 去重数: {len(destroy_name_list)}; 需上传总数: {len(return_path_list)}")
    
    return return_path_list
